package com.easypan.controller;

import com.easypan.annotation.GlobalInterceptor;
import com.easypan.annotation.VerifyParam;
import com.easypan.entity.constants.Constants;
import com.easypan.entity.dto.SessionShareDto;
import com.easypan.entity.dto.SessionWebUserDto;
import com.easypan.entity.enums.FileDelFlagEnums;
import com.easypan.entity.enums.ResponseCodeEnum;
import com.easypan.entity.po.FileInfo;
import com.easypan.entity.po.FileShare;
import com.easypan.entity.po.UserInfo;
import com.easypan.entity.query.FileInfoQuery;
import com.easypan.entity.vo.FileInfoVO;
import com.easypan.entity.vo.PaginationResultVO;
import com.easypan.entity.vo.ResponseVO;
import com.easypan.entity.vo.ShareInfoVO;
import com.easypan.exception.BusinessException;
import com.easypan.service.FileInfoService;
import com.easypan.service.FileShareService;
import com.easypan.service.UserInfoService;
import com.easypan.utils.CopyTools;
import com.easypan.utils.StringTools;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController("webShareController")
@RequestMapping("/showShare")
public class WebShareController extends CommonFileController {

      @Resource
      private FileShareService fileShareService;

      @Resource
      private FileInfoService fileInfoService;

      @Resource
      private UserInfoService userInfoService;


      /**
       * 获取分享登录信息
       *
       * @param session
       * @param shareId
       * @return
       */
      @RequestMapping("/getShareLoginInfo")
      @GlobalInterceptor(checkLogin = false, checkParams = true)
      public ResponseVO getShareLoginInfo(HttpSession session, @VerifyParam(required = true) String shareId) {
            SessionShareDto shareSessionDto = getSessionShareFromSession(session, shareId);
            if (shareSessionDto == null) {
                  return getSuccessResponseVO(null);
            }
            ShareInfoVO shareInfoVO = getShareInfoCommon(shareId);
            //判断是否是当前用户分享的文件
            SessionWebUserDto userDto = getUserInfoFromSession(session);
            if (userDto != null && userDto.getUserId().equals(shareSessionDto.getShareUserId())) {
                  shareInfoVO.setCurrentUser(true);
            } else {
                  shareInfoVO.setCurrentUser(false);
            }
            return getSuccessResponseVO(shareInfoVO);
      }

      /**
       * 获取分享信息
       *
       * @param shareId
       * @return
       */
      @RequestMapping("/getShareInfo")
      @GlobalInterceptor(checkLogin = false, checkParams = true)
      public ResponseVO getShareInfo(@VerifyParam(required = true) String shareId) {
            return getSuccessResponseVO(getShareInfoCommon(shareId));
      }

      private ShareInfoVO getShareInfoCommon(String shareId) {
            FileShare share = fileShareService.getFileShareByShareId(shareId);
            if (null == share || (share.getExpireTime() != null && new Date().after(share.getExpireTime()))) {
                  throw new BusinessException(ResponseCodeEnum.CODE_902.getMsg());
            }
            ShareInfoVO shareInfoVO = CopyTools.copy(share, ShareInfoVO.class);
            FileInfo fileInfo = fileInfoService.getFileInfoByFileIdAndUserId(share.getFileId(), share.getUserId());
            if (fileInfo == null || !FileDelFlagEnums.USING.getFlag().equals(fileInfo.getDelFlag())) {
                  throw new BusinessException(ResponseCodeEnum.CODE_902.getMsg());
            }
            shareInfoVO.setFileName(fileInfo.getFileName());
            UserInfo userInfo = userInfoService.getUserInfoByUserId(share.getUserId());
            shareInfoVO.setNickName(userInfo.getNickName());
            shareInfoVO.setAvatar(userInfo.getQqAvatar());
            shareInfoVO.setUserId(userInfo.getUserId());
            return shareInfoVO;
      }

      /**
       * 校验分享码
       *
       * @param session
       * @param shareId
       * @param code
       * @return
       */
      @RequestMapping("/checkShareCode")
      @GlobalInterceptor(checkLogin = false, checkParams = true)
      public ResponseVO checkShareCode(HttpSession session,
                                       @VerifyParam(required = true) String shareId,
                                       @VerifyParam(required = true) String code) {
            SessionShareDto shareSessionDto = fileShareService.checkShareCode(shareId, code);
            session.setAttribute(Constants.SESSION_SHARE_KEY + shareId, shareSessionDto);
            return getSuccessResponseVO(null);
      }

      /**
       * 获取文件列表
       *
       * @param session
       * @param shareId
       * @return
       */
      @RequestMapping("/loadFileList")
      @GlobalInterceptor(checkLogin = false, checkParams = true)
      public ResponseVO loadFileList(HttpSession session,
                                     @VerifyParam(required = true) String shareId, String filePid) {
            SessionShareDto shareSessionDto = checkShare(session, shareId);
            FileInfoQuery query = new FileInfoQuery();
            if (!StringTools.isEmpty(filePid) && !Constants.ZERO_STR.equals(filePid)) {
                  fileInfoService.checkRootFilePid(shareSessionDto.getFileId(), shareSessionDto.getShareUserId(), filePid);
                  query.setFilePid(filePid);
            } else {
                  query.setFileId(shareSessionDto.getFileId());
            }
            query.setUserId(shareSessionDto.getShareUserId());
            query.setOrderBy("last_update_time desc");
            query.setDelFlag(FileDelFlagEnums.USING.getFlag());
            PaginationResultVO resultVO = fileInfoService.findListByPage(query);
            return getSuccessResponseVO(convert2PaginationVO(resultVO, FileInfoVO.class));
      }


      /**
       * 校验分享是否失效
       *
       * @param session
       * @param shareId
       * @return
       */
      private SessionShareDto checkShare(HttpSession session, String shareId) {
            SessionShareDto shareSessionDto = getSessionShareFromSession(session, shareId);
            if (shareSessionDto == null) {
                  throw new BusinessException(ResponseCodeEnum.CODE_903);
            }
            if (shareSessionDto.getExpireTime() != null && new Date().after(shareSessionDto.getExpireTime())) {
                  throw new BusinessException(ResponseCodeEnum.CODE_902);
            }
            return shareSessionDto;
      }


      /**
       * 获取目录信息
       *
       * @param session
       * @param shareId
       * @param path
       * @return
       */
      @RequestMapping("/getFolderInfo")
      @GlobalInterceptor(checkLogin = false, checkParams = true)
      public ResponseVO getFolderInfo(HttpSession session,
                                      @VerifyParam(required = true) String shareId,
                                      @VerifyParam(required = true) String path) {
            SessionShareDto shareSessionDto = checkShare(session, shareId);
            return super.getFolderInfo(path, shareSessionDto.getShareUserId());
      }

      @RequestMapping("/getFile/{shareId}/{fileId}")
      public void getFile(HttpServletResponse response, HttpSession session,
                          @PathVariable("shareId") @VerifyParam(required = true) String shareId,
                          @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
            SessionShareDto shareSessionDto = checkShare(session, shareId);
            super.getFile(response, fileId, shareSessionDto.getShareUserId());
      }

      @RequestMapping("/ts/getVideoInfo/{shareId}/{fileId}")
      public void getVideoInfo(HttpServletResponse response,
                               HttpSession session,
                               @PathVariable("shareId") @VerifyParam(required = true) String shareId,
                               @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
            SessionShareDto shareSessionDto = checkShare(session, shareId);
            super.getFile(response, fileId, shareSessionDto.getShareUserId());
      }

      @RequestMapping("/createDownloadUrl/{shareId}/{fileId}")
      @GlobalInterceptor(checkLogin = false, checkParams = true)
      public ResponseVO createDownloadUrl(HttpSession session,
                                          @PathVariable("shareId") @VerifyParam(required = true) String shareId,
                                          @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
            SessionShareDto shareSessionDto = checkShare(session, shareId);
            return super.createDownloadUrl(fileId, shareSessionDto.getShareUserId());
      }

      /**
       * 下载
       *
       * @param request
       * @param response
       * @throws Exception
       */
      @RequestMapping("/download/{code}")
      @GlobalInterceptor(checkLogin = false, checkParams = true)
      public void download(HttpServletRequest request, HttpServletResponse response,
                           @PathVariable("code") @VerifyParam(required = true) String code) throws Exception {
            super.download(request, response, code);
      }

      /**
       * 保存分享
       *
       * @param session
       * @param shareId
       * @param shareFileIds
       * @param myFolderId
       * @return
       */
      @RequestMapping("/saveShare")
      @GlobalInterceptor(checkParams = true)
      public ResponseVO saveShare(HttpSession session,
                                  @VerifyParam(required = true) String shareId,
                                  @VerifyParam(required = true) String shareFileIds,
                                  @VerifyParam(required = true) String myFolderId) {
            String shareIdStr=shareId.split(" ")[0];
            SessionShareDto shareSessionDto = checkShare(session, shareIdStr);
            SessionWebUserDto webUserDto = getUserInfoFromSession(session);
            if (shareSessionDto.getShareUserId().equals(webUserDto.getUserId())) {
                  throw new BusinessException("自己分享的文件无法保存到自己的网盘");
            }
            fileInfoService.saveShare(shareSessionDto.getFileId(), shareFileIds, myFolderId, shareSessionDto.getShareUserId(), webUserDto.getUserId());
            return getSuccessResponseVO(null);
      }
}
